package nuonuo.com.view.customview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.ImageView;

import nuonuo.com.view.DimenUtils;
import nuonuo.com.view.R;

/**
 * Created by n6 on 2017/3/2.
 */

public class CircleImageView extends ImageView {
    private Paint mPaint, mBmPaint,mTextPaint;
    private Path mPath;
    private Context mContext;
    private int mHeight, mWidth;
    private Bitmap mBitmap;
    private int mIconRadius;
    private int size;
    private int mBmWidth;
    private int mBmHeight;
    private float scale;
    private boolean flag;

    public CircleImageView(Context context) {
        this(context, null);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(mContext.getResources().getColor(R.color.lightgray));
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);

        mTextPaint = new Paint();
        mTextPaint.setTextSize(DimenUtils.dip2px(mContext,16));

        mBmPaint = new Paint();
    }

    public void setBitmap(Bitmap bm) {
        mBitmap = bm;
        flag = false;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mPath == null) {
            mPath = new Path();
            mWidth = getWidth();
            mHeight = getHeight();
            mIconRadius = mHeight / 9;
            mPath.moveTo(0, 0);
            mPath.lineTo(mWidth, 0);
            mPath.lineTo(mWidth, (float) (mHeight - (1 - Math.sqrt(3) / 2) * mWidth));
            for (int x = mWidth; x >= 0; x--) {
                mPath.lineTo(x, (float) (Math.sqrt(Math.pow(mWidth, 2) - Math.pow(x - mWidth / 2, 2)) + mHeight - mWidth));
            }
            mPath.close();
        }
        if (mBitmap != null && !flag) {
            mBmWidth = mBitmap.getWidth();
            mBmHeight = mBitmap.getHeight();
            size = Math.min(mBmWidth, mBmHeight);
            Matrix matrix = new Matrix();
            scale = (float) ((2 * mIconRadius * 1.0) / size);
            matrix.postScale(scale, scale);
//            mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBmWidth, mBmHeight, matrix, true);
//            mBmWidth = mBitmap.getWidth();
//            mBmHeight = mBitmap.getHeight();
            mBitmap = Bitmap.createBitmap(mBitmap, (mBmWidth - size) / 2, (mBmHeight - size) / 2, size, size);
            BitmapShader shader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            matrix.postTranslate((mWidth / 2 - mIconRadius),mIconRadius / 2);
            shader.setLocalMatrix(matrix);
            mBmPaint.setShader(shader);
            flag = true;
//            canvas.setBitmap();
        }
        canvas.drawPath(mPath, mPaint);
        canvas.drawCircle(mWidth / 2, mIconRadius * 3 / 2, mIconRadius, mBmPaint);
        float length = mTextPaint.measureText("名字");
        canvas.drawText("名字",mWidth / 4.0f - length / 2.0f,(float) (Math.sqrt(Math.pow(mWidth, 2) - Math.pow(mWidth / 4.0f - mWidth / 2, 2)) + mHeight - mWidth),mTextPaint);
        canvas.drawText("名字",3 * mWidth / 4.0f - length / 2.0f,(float) (Math.sqrt(Math.pow(mWidth, 2) - Math.pow(mWidth / 4.0f - mWidth / 2, 2)) + mHeight - mWidth),mTextPaint);
//        canvas.drawBitmap(mBitmap,0,0,null);
//        Canvas mCan = new Canvas(mBitmap);
    }
}
